package com.jvmoutofmemory.octopus.ui;

import com.jvmoutofmemory.octopus.model.FileModel;
import com.jvmoutofmemory.octopus.util.PrintUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class OctopusTablePopMenu extends JPopupMenu {

    private JMenuItem print;

    private MainOctopusFrame mainOctopusFrame;

    public OctopusTablePopMenu(MainOctopusFrame mainOctopusFrame) {
        this.mainOctopusFrame = mainOctopusFrame;
    }

    public void initialize() {
        print = new JMenuItem("打印");
        print.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JTable table = mainOctopusFrame.getFilesTable();
                int[] rows = table.getSelectedRows();
                List<FileModel> modelList = new ArrayList<>();
                for (int i : rows) {
                    modelList.add(mainOctopusFrame.getOctopusTableModel().getFileModel(i));
                }

                PrintUtil.printAll(mainOctopusFrame, modelList, mainOctopusFrame.getPrintIntervalSecond());
            }
        });

        this.add(print);
    }

}
